package com.jcf.springboot.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.FilmInfo;
import com.jcf.springboot.entity.sys.Manager;
import com.jcf.springboot.service.FilmInfoService;
import com.jcf.springboot.service.ManagerService;
import com.jcf.springboot.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@CrossOrigin
public class UploadController {

    @Value("${spring.application.name}")
    private String moduleName;
@Autowired
private ManagerService managerService;
    @Autowired
    private ServletContext servletContext;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private FilmInfoService filmInfoService;

    @RequestMapping("/upload")
    //flag为1表示公告图片,为2表示导演照片，为3表示演员照片，为4表示剧照
    public R upload(MultipartFile file, Integer flag, HttpServletRequest request) throws FileNotFoundException {
        if(StringUtils.isEmpty(file)){
            return R.error().message("文件不能为空");
        }
        String projectName = System.getProperty("user.dir");

        String fileName = file.getOriginalFilename();
        String filePath = "";
        String now = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String mes = "";
        String url = "";
        if(flag == 1){
           filePath = "E:\\graduationProject\\cinema-system-parent\\service\\service-cinema\\image\\notice\\"+now+"\\";
           mes = "上传公告图片";
           url = "http://localhost:9999/image/notice/"+now+"/"+fileName;
        }
        else if(flag == 2){
            filePath = filePath = "E:\\graduationProject\\cinema-system-parent\\service\\service-cinema\\image\\directorPhoto\\";
            mes = "上传导演图片";
            url = "http://localhost:9999/image/directorPhoto/"+"/"+fileName;
        }
        else if(flag == 3){
            filePath = filePath = "E:\\graduationProject\\cinema-system-parent\\service\\service-cinema\\image\\actorPhoto\\";
            mes = "上传演员图片";
            url = "http://localhost:9999/image/actorPhoto/"+"/"+fileName;
        }
        else if(flag == 4){
            filePath = "E:\\graduationProject\\cinema-system-parent\\service\\service-cinema\\image\\stills\\";
            mes = "上传剧照";
            url = "http://localhost:9999/image/stills/"+"/"+fileName;
        }
        else if(flag == 5){
            filePath = System.getProperty("user.dir") + "/"+ moduleName +"/image/filmPoster/";
            System.out.println("absolutePath"+filePath);
            mes = "上传电影海报";


            url = "http://localhost:9999" + request.getContextPath() + "/image/filmPoster"  + "/"+fileName;
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();


            String filmName = request.getParameter("filmName");
            if(!StringUtils.isEmpty(filmName)){
                QueryWrapper<FilmInfo> filmInfoQueryWrapper = new QueryWrapper<>();
                filmInfoQueryWrapper.eq("film_name",filmName);

                FilmInfo one = filmInfoService.getOne(filmInfoQueryWrapper);
                one.setFilmPicture(url);
                filmInfoService.updateById(one);
            }
        }
        else if(flag == 6){
            filePath = System.getProperty("user.dir") + "/"+ moduleName +"/image/member/";
            System.out.println("absolutePath"+filePath);
            mes = "上传客户头像";

            url = "http://localhost:9999/common/image/member"+"/"+fileName;

        }
        else if(flag == 7){

            filePath = System.getProperty("user.dir") + "/"+ moduleName +"/image/user/";
            System.out.println("absolutePath"+filePath);
            mes = "上传用户头像";

            url = "http://localhost:9999/image/user"+"/"+fileName;

            url = "https://jcf-guli-edu.oss-cn-beijing.aliyuncs.com/avatar/"+fileName;
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();


            if(!ObjectUtils.isEmpty(principal) && principal.toString() != "anonymousUser"){
                Manager manager = (Manager) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                System.out.println(manager);
                manager.setAvatar(url);
                managerService.updateById(manager);
            }
        }
        else {
            return R.error().message("未指定上传类型");
        }
        File dest = new File(filePath + fileName);
        //阿里云文件上传
//        uploadService.uploadOss(file);
//        return R.ok().data("url", "https://jcf-guli-edu.oss-cn-beijing.aliyuncs.com/avatar/"+fileName).message(mes+"上传成功");
        if(!dest.exists()){
            dest.mkdirs();
        }
        System.out.println("dest:"+ dest.getAbsolutePath());
        try {/*E:\graduationProject\cinema-system-parent\service\service-cinema\target\classes\static\image*/
            file.transferTo(dest);
            return R.ok().data("url",url).message(mes+"上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error().message(mes+"上传失败");

    }
}
